package bb.lanxing.lib.devices.bici;

//import com.igexin.push.f.r;
import bb.lanxing.lib.devices.sprint.Commands;
import bb.lanxing.lib.devices.utils.CRC;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public abstract class BLECommand {
    public static final int CMD_HEADER_LEN = 8;
    public static final int CMD_TYPE_DEFAULT = 0;
    public static final int CMD_TYPE_ERROR = 2;
    public static final int CMD_TYPE_RESPONSE = 1;
    public static final byte KEY_SEQUENCE = 7;
    public static final byte L1_ACK_DEFAULT = 0;
    public static final byte L1_ACK_RESPONSE = 1;
    public static final byte L1_ERR_DEFAULT = 0;
    public static final byte L1_ERR_ERROR = 1;
    public static final byte L1_VERSION = 1;
    public static final byte LIGHT_ITEM_LEN = 4;
    public static final byte LIGHT_TYPE_HEAD = 1;
    public static final byte LIGHT_TYPE_WIDTH = 2;
    public static final byte MAGIC_DEFAULT = -85;
    public static short sequence;
    protected byte commandId;
    protected byte l1AckFlag;
    protected short l1CRC;
    protected byte l1ErrFlag;
    protected byte l1Flag;
    protected byte l1Magic;
    protected short l1PayloadLen;
    protected short l1SequenceId;
    protected byte l1Version;
    protected Charset textEncoding = StandardCharsets.UTF_8;//r.b

    private static byte buildFlag(int i) {
        if (i != 0) {
            return i != 1 ? i != 2 ? (byte) 1 : (byte) 49 : Commands.VERIFICATION_FAILED;
        }
        return (byte) 1;
    }

    protected abstract byte[] encodeCMDBody(Map<Byte, Object> map);

    public abstract void parseBody(byte[] bArr);

    public BLECommand() {
    }

    public BLECommand(byte[] bArr) {
        parseBody(bArr);
    }

    public byte[] buildCommand(int i, Map<Byte, Object> map) {
        byte[] encodeCMDBody = encodeCMDBody(map);
        int length = encodeCMDBody.length;
        ByteBuffer allocate = ByteBuffer.allocate(length + 8);
        allocate.order(ByteOrder.BIG_ENDIAN);
        allocate.put(MAGIC_DEFAULT);
        allocate.put(buildFlag(i));
        allocate.putShort((short) length);
        allocate.putShort(CRC.getCrc16(encodeCMDBody));
        allocate.putShort(getSequence());
        allocate.put(encodeCMDBody);
        return allocate.array();
    }

    public static short getSequence() {
        if (sequence == 65535) {
            sequence = (short) 0;
        }
        short s = (short) (sequence + 1);
        sequence = s;
        return s;
    }

    public static byte[] buildErrorOrSuccessResponse(int i, Map<Byte, Object> map) {
        ByteBuffer allocate = ByteBuffer.allocate(8);
        allocate.order(ByteOrder.BIG_ENDIAN);
        allocate.put(MAGIC_DEFAULT);
        allocate.put(buildFlag(i));
        allocate.putShort((short) 0);
        allocate.putShort((short) 0);
        allocate.putShort((Short) map.get((byte) 7));
        return allocate.array();
    }

    public byte getCommandId() {
        return this.commandId;
    }

    public static Map<Byte, byte[]> parseLight(byte[] bArr) {
        HashMap hashMap = new HashMap();
        for (int i = 0; i < bArr.length; i += 4) {
            byte[] bArr2 = new byte[4];
            System.arraycopy(bArr, i, bArr2, 0, 4);
            hashMap.put(bArr[i], bArr2);
        }
        return hashMap;
    }

    public byte getL1Magic() {
        return this.l1Magic;
    }

    public void setL1Magic(byte b) {
        this.l1Magic = b;
    }

    public byte getL1Flag() {
        return this.l1Flag;
    }

    public void setL1Flag(byte b) {
        this.l1Flag = b;
    }

    public byte getL1ErrFlag() {
        return this.l1ErrFlag;
    }

    public void setL1ErrFlag(byte b) {
        this.l1ErrFlag = b;
    }

    public byte getL1AckFlag() {
        return this.l1AckFlag;
    }

    public void setL1AckFlag(byte b) {
        this.l1AckFlag = b;
    }

    public byte getL1Version() {
        return this.l1Version;
    }

    public void setL1Version(byte b) {
        this.l1Version = b;
    }

    public short getL1PayloadLen() {
        return this.l1PayloadLen;
    }

    public void setL1PayloadLen(short s) {
        this.l1PayloadLen = s;
    }

    public short getL1CRC() {
        return this.l1CRC;
    }

    public void setL1CRC(short s) {
        this.l1CRC = s;
    }

    public short getL1SequenceId() {
        return this.l1SequenceId;
    }

    public void setL1SequenceId(short s) {
        this.l1SequenceId = s;
    }

    public void setCommandId(byte b) {
        this.commandId = b;
    }
}
